﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc.Html;
using Lbb.Configuration;
using System.Configuration;
using System.Web.Routing;

namespace System.Web.Mvc
{
    /// <summary>
    /// MVC的Html Helper的扩展
    /// 作者：刘勇
    /// 日期：2012年5月17日
    /// </summary>
    public static class ExtendedHtmlHelper
    {
        /// <summary>
        /// 生成CheckBox的组合
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="items"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items, string @class = "", bool selecteAllBtn = true, string labelID = "selectall")
        {
            string html = "";
            foreach (SelectListItem i in items)
            {
                html += string.Format("<input id='{0}_{1}' type='checkbox' name='{0}' value='{1}' {2} class='{4}'/><label for='{0}_{1}'>{3}</label>",
                                        name,
                                        i.Value,
                                        i.Selected ? "checked='checked'" : "",
                                        i.Text,
                                        @class
                                    );
            }
            if (selecteAllBtn)
            {
                //    html += "<a href='javascript:void(0)' class='selectall' rel='" + name + "'>全选</a>";
                html += "<label for='" + labelID + "' class='selectall'>全选</label><input type='checkbox' class='selectall' id='" + labelID + "' rel='" + name + "' />";
            }
            return MvcHtmlString.Create(html);
        }
        public static ConfigItem GetConfig()
        {
            return ConfigItem.Load(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings.Get("ConfigPath")));
        }

        /// <summary>
        /// 下拉菜单
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="sectionName">节点名称</param>
        /// <param name="name">提交name</param>
        /// <param name="selectedValue">选中的项，支持“,”</param>
        /// <returns></returns>
        public static MvcHtmlString DropDownList(this HtmlHelper helper, string name, string sectionName, string selectedValue, string @class = "", IEnumerable<SelectListItem> pExtItems = null, IEnumerable<SelectListItem> nExtItems = null)
        {
            ConfigItem item = GetConfig();
            List<SelectListItem> itemList = item.Get(sectionName).ToSelectItemList(selectedValue, pExtItems: pExtItems, nExtItems: nExtItems);
            return helper.DropDownList(name, itemList, new { @class = @class });
        }


        /// <summary>
        /// 多选
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="sectionName">节点名称</param>
        /// <param name="name">提交name</param>
        /// <param name="selectedValues">选中的项，支持“,”/param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, string sectionName, string selectedValues, string @class = "", bool selecteAllBtn = true, string labelID = "selectall")
        {
            ConfigItem item = GetConfig();
            List<SelectListItem> itemList = item.Get(sectionName).ToSelectItemList(selectedValues);
            return helper.CheckBoxList(name, itemList, @class, selecteAllBtn, labelID);
        }

        /// <summary>
        /// 根据楼盘选择的拓展
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="sectionName"></param>
        /// <param name="selectedProperty"></param>
        /// <param name="selectedValues"></param>
        /// <param name="class"></param>
        /// <param name="selecteAllBtn"></param>
        /// <returns></returns>
        public static MvcHtmlString ProDropDownList(this HtmlHelper helper, string name, string sectionName, string selectedProperty, string selectedValues, string @class = "")
        {
            ConfigItem item = GetConfig();
            List<SelectListItem> itemList = item.Get(sectionName).SelectedItemList(selectedProperty, selectedValues);
            return helper.DropDownList(name, itemList, new { @class = @class });
        }
        /// <summary>
        /// 显示选中的结果
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="sectionName"></param>
        /// <param name="selectedValues"></param>
        /// <returns></returns>
        public static MvcHtmlString ToLabel(this HtmlHelper helper, string sectionName, string selectedValues)
        {
            if (!string.IsNullOrEmpty(selectedValues))
            {
                ConfigItem item = GetConfig();
                return helper.Label(item.Get(sectionName).Items.Where(d => d.Value.Contains(selectedValues)).FirstOrDefault().Name);
            }
            else
            {
                return MvcHtmlString.Create("暂无");
            }
        }
    }
}